#include "camera_utils.h"
#include <signal.h>

// 全局变量，用于线程控制
volatile int running = 1;

// 信号处理函数
void signal_handler(int sig) {
    (void)sig;  // 避免未使用参数的警告
    running = 0;
    printf("\n接收到中断信号，正在停止程序...\n");
}

// 打印使用说明
void print_usage(char *program_name) {
    printf("使用方法: %s [选项]\n", program_name);
    printf("选项:\n");
    printf("  -h, --help           显示帮助\n");
    printf("  -1                   测试摄像头 1 (IP: 192.168.1.60)\n");
    printf("  -2                   测试摄像头 2 (IP: 192.168.1.64)\n");
    printf("  -3                   测试摄像头 3 (IP: 192.168.1.65)\n");
    printf("  -a, --all            测试所有摄像头\n");
    printf("  -c, --continuous     连续模式 (每10秒捕获一次)\n");
    printf("  -s, --single         单次模式 (只捕获一次)\n");
}

int main(int argc, char *argv[]) {
    // 初始化摄像头配置
    CameraConfig cam1 = {
        .ip = "192.168.1.60",
        .username = "admin",
        .password = "hitmc401",
        .save_dir = "camera",
        .prefix = "cam400w"
    };
    
    CameraConfig cam2 = {
        .ip = "192.168.1.64",
        .username = "admin",
        .password = "hitmc401",
        .save_dir = "camera",
        .prefix = "cam800w_left"
    };
    
    CameraConfig cam3 = {
        .ip = "192.168.1.65",
        .username = "admin",
        .password = "hitmc401",
        .save_dir = "camera",
        .prefix = "cam800w_right"
    };
    
    // 线程ID
    pthread_t tid1, tid2, tid3;
    
    // 解析命令行参数
    int test_cam1 = 0, test_cam2 = 0, test_cam3 = 0;
    int continuous_mode = 0;
    int i;
    
    // 如果没有参数，显示使用方法
    if (argc < 2) {
        print_usage(argv[0]);
        return 1;
    }
    
    // 解析命令行参数
    for (i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
            print_usage(argv[0]);
            return 0;
        } else if (strcmp(argv[i], "-1") == 0) {
            test_cam1 = 1;
        } else if (strcmp(argv[i], "-2") == 0) {
            test_cam2 = 1;
        } else if (strcmp(argv[i], "-3") == 0) {
            test_cam3 = 1;
        } else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--all") == 0) {
            test_cam1 = test_cam2 = test_cam3 = 1;
        } else if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--continuous") == 0) {
            continuous_mode = 1;
        } else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--single") == 0) {
            continuous_mode = 0;
        } else {
            printf("未知选项: %s\n", argv[i]);
            print_usage(argv[0]);
            return 1;
        }
    }
    
    // 初始化libcurl
    curl_global_init(CURL_GLOBAL_ALL);
    
    // 设置信号处理
    signal(SIGINT, signal_handler);
    
    printf("海康威视摄像头测试程序启动\n");
    
    // 配置所有摄像头的时间与系统时间同步
    printf("正在配置摄像头时间...\n");
    if (test_cam1) {
        printf("配置摄像头 1 (IP: %s) 时间...\n", cam1.ip);
        if (set_camera_time(&cam1) != 0) {
            fprintf(stderr, "配置摄像头 1 时间失败\n");
        }
    }
    
    if (test_cam2) {
        printf("配置摄像头 2 (IP: %s) 时间...\n", cam2.ip);
        if (set_camera_time(&cam2) != 0) {
            fprintf(stderr, "配置摄像头 2 时间失败\n");
        }
    }
    
    if (test_cam3) {
        printf("配置摄像头 3 (IP: %s) 时间...\n", cam3.ip);
        if (set_camera_time(&cam3) != 0) {
            fprintf(stderr, "配置摄像头 3 时间失败\n");
        }
    }
    printf("摄像头时间配置完成\n");
    
    // 单次模式
    if (!continuous_mode) {
        if (test_cam1) {
            printf("测试摄像头 1 (IP: %s)...\n", cam1.ip);
            if (capture_image(&cam1) != 0) {
                fprintf(stderr, "从摄像头 1 捕获图像失败\n");
            }
        }
        
        if (test_cam2) {
            printf("测试摄像头 2 (IP: %s)...\n", cam2.ip);
            if (capture_image(&cam2) != 0) {
                fprintf(stderr, "从摄像头 2 捕获图像失败\n");
            }
        }
        
        if (test_cam3) {
            printf("测试摄像头 3 (IP: %s)...\n", cam3.ip);
            if (capture_image(&cam3) != 0) {
                fprintf(stderr, "从摄像头 3 捕获图像失败\n");
            }
        }
    } else {
        // 连续模式 - 创建线程
        if (test_cam1) {
            printf("启动摄像头 1 (IP: %s) 连续捕获线程...\n", cam1.ip);
            if (pthread_create(&tid1, NULL, camera_thread, &cam1) != 0) {
                fprintf(stderr, "创建摄像头 1 线程失败\n");
                return -1;
            }
        }
        
        if (test_cam2) {
            printf("启动摄像头 2 (IP: %s) 连续捕获线程...\n", cam2.ip);
            if (pthread_create(&tid2, NULL, camera_thread, &cam2) != 0) {
                fprintf(stderr, "创建摄像头 2 线程失败\n");
                return -1;
            }
        }
        
        if (test_cam3) {
            printf("启动摄像头 3 (IP: %s) 连续捕获线程...\n", cam3.ip);
            if (pthread_create(&tid3, NULL, camera_thread, &cam3) != 0) {
                fprintf(stderr, "创建摄像头 3 线程失败\n");
                return -1;
            }
        }
        
        // 等待信号中断
        printf("程序正在运行，按 Ctrl+C 停止...\n");
        while (running) {
            sleep(1);
        }
    }
    
    // 清理libcurl
    curl_global_cleanup();
    
    printf("程序已退出\n");
    return 0;
} 